package com.angel.webapp.manager;

import com.angel.arquitectura.exception.NonBusinessException;
import com.angel.io.descriptor.ExportedFileDescriptor;
import com.angel.io.processors.exports.ExportFileProcessorCommand;
import com.angel.io.processors.exports.ExportRowProcessorCommand;
import com.angel.resourceBundle.results.KeyValueResult;
import com.angel.webapp.application.ApplicationExportFileCommands;
import com.angel.webapp.application.ApplicationExportRowCommands;
import com.angel.webapp.application.ApplicationSearchStrategyCommands;
import com.angel.webapp.constants.WebappExportServicesNames;
import com.angel.webapp.search.SearchStrategyCommand;
import com.angel.webapp.services.ExportFilesService;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import java.io.Serializable;
import java.util.List;

/**
 * @author William
 * @since 03/February/2009
 */
public class ExportManager implements Serializable {

    private String searchStrategyCommandName;

    private String exportRowCommandName;

    private String exportFileCommandName;

    public ExportManager(String searchStrategyCommandName, String exportRowCommandName, String exportFileCommandName){
        super();
        this.searchStrategyCommandName = searchStrategyCommandName;
        this.exportRowCommandName = exportRowCommandName;
        this.exportFileCommandName = exportFileCommandName;
    }

    public ExportManager(){
        super();
    }

    public String getSearchStrategyCommandName() {
        return searchStrategyCommandName;
    }

    public void setSearchStrategyCommandName(String searchStrategyCommandName) {
        this.searchStrategyCommandName = searchStrategyCommandName;
    }

    public String getExportRowCommandName() {
        return exportRowCommandName;
    }

    public void setExportRowCommandName(String exportRowCommandName) {
        this.exportRowCommandName = exportRowCommandName;
    }

    public String getExportFileCommandName() {
        return exportFileCommandName;
    }

    public void setExportFileCommandName(String exportFileCommandName) {
        this.exportFileCommandName = exportFileCommandName;
    }

    public void validate() throws NonBusinessException {
        Boolean containsFileCommand = ApplicationExportFileCommands.createInstance().containsExportFileCommand(this.getExportFileCommandName());
        Boolean containsRowCommand = ApplicationExportRowCommands.createInstance().containsExportRowCommand(this.getExportRowCommandName());
        Boolean containsSearchStrategyCommand = ApplicationSearchStrategyCommands.createInstance().containsSearchStrategyCommand(this.getSearchStrategyCommandName());
        if(!containsFileCommand || !containsRowCommand || !containsSearchStrategyCommand){
            String errorMessage = "Commands necesaries for export process are not present: " +
                    "Export row command name: " + this.getExportRowCommandName() + " - Foundt " + containsRowCommand.toString() +
                    "Export file command name: " + this.getExportFileCommandName() + " - Foundt " + containsFileCommand.toString() +
                    "Search strategy command name: " + this.getSearchStrategyCommandName() + " - Foundt " + containsSearchStrategyCommand.toString();
            throw new NonBusinessException(errorMessage);
        }
    }


    public ExportedFileDescriptor export(KeyValueResult searchParameters) {
        ExportFileProcessorCommand fileProcessorCommand = ApplicationExportFileCommands.createInstance().findExportFileCommand(this.getExportFileCommandName());
        ExportRowProcessorCommand rowProcessorCommand = ApplicationExportRowCommands.createInstance().findExportRowCommand(this.getExportRowCommandName());
        SearchStrategyCommand searchStrategyCommand = ApplicationSearchStrategyCommands.createInstance().findSearchStrategyCommand(this.getSearchStrategyCommandName());

        List<?> objectsToExport = searchStrategyCommand.findObjectToExport(searchParameters);

        ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(flex.messaging.FlexContext.getServletConfig().getServletContext());
        ExportFilesService exportFilesService = (ExportFilesService) appContext.getBean(WebappExportServicesNames.EXPORT_FILES_SERVICE_NAME);

        ExportedFileDescriptor exportedFileDescriptor = exportFilesService.doExportFile(objectsToExport, fileProcessorCommand, rowProcessorCommand);

        return exportedFileDescriptor;
    }
}